<template>
  <div class="app-container">
    <div class="header">
      <el-form :model="queryParams" ref="queryRef" :inline="true" class="demo-form-inline">
        <el-form-item label="操作人">
          <el-input v-model="queryParams.name" placeholder="请输入操作人" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="操作IP/地点">
          <el-input v-model="queryParams.keywords" placeholder="请输入操作IP或地点" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="操作时间">
          <el-date-picker v-model="dateRange" type="daterange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime" />
        </el-form-item>
        <div class="button-group" style="width: 256px;">
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </div>
      </el-form>
    </div>
    <el-card class="box-card">
      <!-- Search Form -->
      <div class="f">
        <h3 class="util">
          操作日志
        </h3>
      </div>
      <!-- Operation Log Table -->
      <el-table v-loading="loading" :data="logList" style="width: 100%" border>
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="createTime" label="操作时间" width="180" />
        <el-table-column prop="createUserName" label="操作人" />
        <el-table-column prop="description" label="操作内容" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
              {{ scope.row.status === 1 ? '成功' : '失败' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="ip" label="操作IP" show-overflow-tooltip />
        <el-table-column prop="address" label="操作地点" show-overflow-tooltip />
        <el-table-column prop="timeTaken" label="耗时" />
        <el-table-column prop="ip" label="IP地址" width="130" show-overflow-tooltip />
        <el-table-column prop="address" label="区域" width="150" show-overflow-tooltip />
        <el-table-column prop="timeTaken" label="耗时" width="100">

          <template #default="scope">
            {{ scope.row.timeTaken }}ms
          </template>
        </el-table-column>
        <el-table-column prop="browser" label="浏览器" />
        <el-table-column prop="os" label="终端系统" />
      </el-table>

      <!-- Pagination -->
      <div class="pagination-container">
        <el-pagination v-model:current-page="queryParams.pageNum" v-model:page-size="queryParams.pageSize"
          :page-sizes="[10, 20, 30, 50]" :total="total" layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { getOperation } from '@/api/logs'

// 状态变量
const loading = ref(false)
const logList = ref([])
const total = ref(0)
const dateRange = ref([])
const defaultTime = [
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 1, 1, 23, 59, 59)
]

// 查询参数
const queryParams = reactive({
  name: '',
  keywords: '',
  masterDeptId: '',
  startTime: '',
  endTime: '',
  pageNum: 1,
  pageSize: 10
})

// 查询操作日志数据
const getList = async () => {
  loading.value = true
  try {
    // 处理时间范围
    if (dateRange.value && dateRange.value.length === 2) {
      queryParams.startTime = dateRange.value[0]
      queryParams.endTime = dateRange.value[1]
    }

    const res = await getOperation(queryParams)

    logList.value = res.data.list
    total.value = res.data.total

  } catch (error) {
    console.error('获取操作日志失败:', error)
  } finally {
    loading.value = false
  }
}

// 搜索按钮操作
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置按钮操作
const resetQuery = () => {
  dateRange.value = []
  Object.assign(queryParams, {
    name: '',
    keywords: '',
    masterDeptId: '',
    startTime: '',
    endTime: '',
    pageNum: 1,
    pageSize: 10
  })
  handleQuery()
}

// 分页大小改变
const handleSizeChange = (val) => {
  queryParams.pageSize = val
  getList()
}

// 页码改变
const handleCurrentChange = (val) => {
  queryParams.pageNum = val
  getList()
}

// 初始化
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;

  .header {
    display: flex;
    padding: 16px 16px 0 16px;
    margin-bottom: 16px;
    border-radius: 4px;
    background: white;
    box-shadow: 0 0 12px rgb(0 0 0 / 5%);

    .demo-form-inline {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }

    .button-group {
      display: flex;
      gap: 10px;
      margin-bottom: 16px;
    }
  }
}

.f {
  display: flex;
  justify-content: space-between;

}

.util {
  display: flex;
  align-items: center;
  --tw-text-opacity: 1;
  color: rgb(71 85 105 / var(--tw-text-opacity));
  font-size: 18px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>